/**
 * Created by LinJe on 2015/9/22.
 * 全局ui：登陆提示 弹窗
 */
(function( $, window ) {

    window.globalLayer = window.globalLayer || {};


    /**
     * 调用实例
     * window.globalLayer.openComment({'callBack':function() {
            console.log('登陆提示窗已关闭');
       }});
     * @param settings
     */
    window.globalLayer.openChannel = function( settings ) {

        var channelArgs;

        //如果是第一次调用，加载弹窗所需html和js变量
        if ( !this.channelArgs ) {
            $('body').append('\
                <div class="layer_channel">\
                    <div class="popup-bg"></div>\
                    <div class="popup release-ugc" style="height:410px;margin-top:-205px;">\
                        <div class="content">\
                            <div class="title clearfix"><div class="text">添加购买渠道</div><div class="btn"><span class="close" title="关闭"></span></div></div>\
                            <div class="from channel">\
                                <input name="" type="text" placeholder="可以购买此商品的网址">\
                                <textarea name="" placeholder="你对此购买渠道的简单介绍"></textarea>\
                                <div class="text-tips">剩余100字</div>\
                                <div class="button"><button type="button" class="button-default button-disabled">发布</button></div>\
                            </div>\
                        </div>\
                    </div>\
                </div>\
            ');

            channelArgs = this.channelArgs = {};
            channelArgs.$channelWrap = $('.layer_channel');
            channelArgs.$colse = channelArgs.$channelWrap.find('.title .btn');
            channelArgs.$publish = channelArgs.$channelWrap.find('.button .button-default');
            channelArgs.$textTips = channelArgs.$channelWrap.find('.text-tips');
            channelArgs.$input = channelArgs.$channelWrap.find('.from input');
            channelArgs.$textarea = channelArgs.$channelWrap.find('.from textarea');


            //输入框
            var txtAreaAllow = false;
            channelArgs.$textarea.on('keydown', function() {
                //限制字数
                $.maxTextLen({
                    'field': channelArgs.$textarea,
                    'countfield': channelArgs.$textTips,
                    'maxlimit': 100,
                    'callBack': function(data) {
                        //是否激活发布按钮
                        if ( data && channelArgs.$input.val().length > 0 ) {
                            channelArgs.$publish.removeClass('button-disabled');
                        } else {
                            channelArgs.$publish.addClass('button-disabled');
                        }
                        txtAreaAllow = data;
                    }
                });
            });
            channelArgs.$textarea.on('keyup', function() {
                //限制字数
                $.maxTextLen({
                    'field': channelArgs.$textarea,
                    'countfield': channelArgs.$textTips,
                    'maxlimit': 100,
                    'callBack': function(data) {
                        //是否激活发布按钮
                        if ( data && channelArgs.$input.val().length > 0 ) {
                            channelArgs.$publish.removeClass('button-disabled');
                        } else {
                            channelArgs.$publish.addClass('button-disabled');
                        }
                        txtAreaAllow = data;
                    }
                });
            });
            channelArgs.$input.on('keydown', function() {
                //限制字数
                if ( txtAreaAllow && channelArgs.$input.val().length > 0 ) {
                    channelArgs.$publish.removeClass('button-disabled');
                } else {
                    channelArgs.$publish.addClass('button-disabled');
                }
            });
            channelArgs.$input.on('keyup', function() {
                //限制字数
                if ( txtAreaAllow && channelArgs.$input.val().length > 0 ) {
                    channelArgs.$publish.removeClass('button-disabled');
                } else {
                    channelArgs.$publish.addClass('button-disabled');
                }
            });


            //点击取消或者X 关闭弹窗
            channelArgs.$colse.on('click', function() {
                window.globalLayer.closeChannel(function(){
                    channelArgs.callBack({'url':'', 'content':''});
                });
            });

            //点击 发布按钮
            channelArgs.$publish.on('click', function() {
                if ( !$(this).hasClass('button-disabled') ) {
                    var url = channelArgs.$input.val();
                    if ( !$.isUrl(url) ) {
                        window.globalLayer.openWarning({'autoClose':true, time: 1000, text:'网址格式不正确'});
                        return;
                    }
                    window.globalLayer.closeChannel(function(){
                        //回调
                        channelArgs.callBack({'url':url, 'content':channelArgs.$textarea.val()});
                        //还原
                        channelArgs.$input.val('');
                        channelArgs.$textarea.val('');
                        channelArgs.$textTips.removeClass('red').text('剩余500字');
                        channelArgs.$publish.addClass('button-disabled');
                    });
                }
            });
        }

        channelArgs = this.channelArgs;
        $.extend(channelArgs, settings);

        channelArgs.$channelWrap.css({'opacity':'0', 'display':'block'}).stop().animate({ 'opacity': 1 });
    };


    window.globalLayer.closeChannel = function( callBack ) {

        this.channelArgs.$channelWrap.stop().animate({ 'opacity': 0 }, 500, '', function() {
            $(this).hide();
            callBack && callBack();
        });

    };

})( jQuery, window );